﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Data.Objects;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace EnglishAuction.BUS
{
    public class SanPhamBUS
    {
        // Search
        public static List<SanPham> Search(string key)
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2)
                        if (item.NgayLenSan.Value.CompareTo(DateTime.Now) < 0)
                            if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) > 0)
                                if(item.TenSanPham.Contains(key))
                                    lst.Add(item);
            }

            return lst;
        }


        // Admin
        public static List<SanPham> GetListDangDauGiaAdmin()
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2)
                        if (item.NgayLenSan.Value.CompareTo(DateTime.Now) < 0)
                            if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) > 0)
                                lst.Add(item);
            }

            return lst;
        }

        public static List<SanPham> GetListDaDauGiaThanhCongAdmin()
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2)
                        if (item.NgayLenSan.Value.CompareTo(DateTime.Now) < 0)
                            if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) < 0)
                                if (NguoiThangCuocBUS.CheckExist(item.Id) != null)
                                    lst.Add(item);
            }

            return lst;
        }

        public static List<SanPham> GetListDaDauGiaThatBaiAdmin()
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2)
                        if (item.NgayLenSan.Value.CompareTo(DateTime.Now) < 0)
                            if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) < 0)
                                if (NguoiThangCuocBUS.CheckExist(item.Id) == null)
                                    lst.Add(item);
            }

            return lst;
        }

        //
        public static NguoiThangCuoc GetIdUserRating(int sanPhamId)
        {
            using (var context = new EnglishAuctionEntities())
            {
                var sanPham = context.SanPhams.Where(s => s.Id == sanPhamId).FirstOrDefault();
                if (sanPham.TinhTrang == 2 && sanPham.NgayLenSan.Value.AddDays(sanPham.ThoiGianDauGia.Value).CompareTo(DateTime.Now) < 0)
                {
                    NguoiThangCuoc ntc = NguoiThangCuocBUS.CheckExist(sanPhamId);
                    if (ntc != null)
                    {
                        return ntc;
                    }
                }
            }

            return null;
        }

        public static List<SanPham> GetListDaDauGiaThanhCong(int userId)
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2 && item.NguoiDungId == userId)
                        if (item.NgayLenSan.Value.CompareTo(DateTime.Now) < 0)
                            if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) < 0)
                                if (NguoiThangCuocBUS.CheckExist(item.Id) != null)
                                    lst.Add(item);
            }

            return lst;
        }

        public static List<SanPham> GetListDaDauGiaThatBai(int userId)
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2 && item.NguoiDungId == userId)
                        if (item.NgayLenSan.Value.CompareTo(DateTime.Now) < 0)
                            if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) < 0)
                                if (NguoiThangCuocBUS.CheckExist(item.Id) == null)
                                    lst.Add(item);
            }

            return lst;
        }


        public static List<SanPham> GetAllDaDauGia()
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2)
                        if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) < 0)
                            lst.Add(item);
            }

            return lst;
        }

        public static List<SanPham> GetListDaDuyet(int nguoiDungId)
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2 && item.NguoiDungId == nguoiDungId)
                        //if (item.NgayLenSan.Value.CompareTo(DateTime.Now) > 0)
                        lst.Add(item);
            }

            return lst;
        }

        public static List<SanPham> GetListDangDauGia(int nguoiDungId)
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2 && item.NguoiDungId == nguoiDungId)
                        if (item.NgayLenSan.Value.CompareTo(DateTime.Now) < 0)
                            if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) > 0)
                                lst.Add(item);
            }

            return lst;
        }

        public static List<SanPham> GetAll()
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.SanPhams)
                    if (item.TinhTrang == 2)
                        if (item.NgayLenSan.Value.CompareTo(DateTime.Now) < 0)
                            if (item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value).CompareTo(DateTime.Now) > 0)
                                lst.Add(item);
            }

            return lst;
        }

        public static SanPham GetById(int id)
        {
            using (var context = new EnglishAuctionEntities())
            {
                return context.SanPhams.Find(id);
            }
        }

        public static SanPham GetDetailById(int id)
        {
            using (var context = new EnglishAuctionEntities())
            {
                return context.SanPhams.Include("LoaiSanPham").Where(u => u.Id == id).FirstOrDefault();
            }
        }

        public static SanPham GetUserByID(int id)
        {
            using (var context = new EnglishAuctionEntities())
            {
                return context.SanPhams.Include("NguoiDung").Where(u => u.Id == id && u.NguoiDung.Id == u.NguoiDungId).FirstOrDefault();
            }
        }

        public static List<SanPham> GetByUser(NguoiDung nguoiDung)
        {
            using (var context = new EnglishAuctionEntities())
            {
                return context.SanPhams.Where(u => u.NguoiDungId == nguoiDung.Id && u.TinhTrang == 1).ToList();
            }
        }

        public static List<SanPham> GetProductByStatus(int tinhTrang, int loaiSanPhamId)
        {
            using (var context = new EnglishAuctionEntities())
            {
                if (loaiSanPhamId == -1)
                    return context.SanPhams.Where(u => u.TinhTrang == tinhTrang).ToList();
                else
                    return context.SanPhams.Where(u => u.TinhTrang == tinhTrang && u.LoaiSanPhamId == loaiSanPhamId).ToList();

            }
        }

        public static bool CheckProductOfUser(int id, NguoiDung nguoiDung)
        {
            using (var context = new EnglishAuctionEntities())
            {
                var temp = context.SanPhams.Where(u => u.NguoiDungId == nguoiDung.Id && u.Id == id);
                if (temp != null && temp.Count() > 0)
                    return true;
                return false;
            }
        }

        public static DetailProduct GetDetailsProduct(int id)
        {
            using (var context = new EnglishAuctionEntities())
            {
                DauGia dauGia = context.DauGias.FirstOrDefault(u => u.SanPhamId == id);
                if (dauGia != null)
                {
                    //Can lay tat ca thuoc tinh 3 bang SanPham,DauGia,NguoiDung
                    var kq = from sp in context.SanPhams
                             from dg in context.DauGias
                             from nd in context.NguoiDungs
                             where sp.Id == id && sp.Id == dg.SanPhamId && dg.NguoiDungId == nd.Id
                             select new DetailProduct
                             {
                                 TenSanPham = sp.TenSanPham,
                                 GiaCaoNhatHienTai = (double)dg.GiaHienTai,
                                 GiaKhoiDiem = (double)sp.GiaSan,
                                 HinhAnh = sp.HinhAnh,
                                 NguoiCoGiaCaoNhat = nd.HoTen,
                                 ThoiGian = (DateTime)dg.ThoiGian,
                                 DiaChi = nd.DiaChi,
                                 SDT = nd.SoDienThoai,
                                 NoiDung = sp.MoTa,
                                 Countdown = "txtCountdown"
                             };
                    return kq.OrderByDescending(u => u.GiaCaoNhatHienTai).Skip(0).Take(1).Single();
                }
                else
                {
                    var kq = from sp in context.SanPhams
                             where sp.Id == id
                             select new DetailProduct
                             {
                                 TenSanPham = sp.TenSanPham,
                                 GiaKhoiDiem = (double)sp.GiaSan,
                                 GiaCaoNhatHienTai = (double)sp.GiaSan,
                                 HinhAnh = sp.HinhAnh,
                                 NguoiCoGiaCaoNhat = "Hiện Chưa Có Ai Đấu Giá",
                                 ThoiGian = (DateTime)sp.NgayLenSan,
                                 DiaChi = null,
                                 SDT = null,
                                 NoiDung = sp.MoTa
                             };
                    return kq.FirstOrDefault();
                }
            }
        }

        public static int CheckProduct(int id)
        {
            //Ham nay kiem tra xem san pham nay la san pham dang dau hay het dau
            //1 dang dau - 0 het dau
            using (var context = new EnglishAuctionEntities())
            {
                SanPham existing = context.SanPhams.Find(id);
                var timeNow = DateTime.Now;
                double timeAuction = existing.ThoiGianDauGia.Value;
                var dateLine = existing.NgayLenSan.Value.AddDays(timeAuction);
                if (dateLine > timeNow && existing.NgayLenSan <= timeNow)
                {
                    return 1;
                }
                return 0;
            }
        }

        public static List<SanPham> GetProductAuction()
        {
            using (var context = new EnglishAuctionEntities())
            {
                List<SanPham> listSanPham = new List<SanPham>();
                var timeNow = DateTime.Now;
                foreach (var item in context.SanPhams)
                {
                    double timeAuction = item.ThoiGianDauGia.Value;
                    var dateLine = item.NgayLenSan.Value.AddDays(timeAuction);
                    if (dateLine > timeNow && item.NgayLenSan <= timeNow)
                    {
                        listSanPham.Add(item);
                    }
                }
                return listSanPham;
            }
        }

        public static List<SanPham> GetProductPass()
        {
            using (var context = new EnglishAuctionEntities())
            {
                List<SanPham> listSanPham = new List<SanPham>();
                var timeNow = DateTime.Now;
                foreach (var item in context.SanPhams.Where(p => p.NgayLenSan != null))
                {
                    double timeAuction = item.ThoiGianDauGia.Value;
                    var dateLine = item.NgayLenSan.Value.AddDays(timeAuction);
                    if (dateLine < timeNow)
                    {
                        listSanPham.Add(item);
                    }
                }
                return listSanPham;
            }
        }

        public static bool Add(SanPham sanPham)
        {
            using (var context = new EnglishAuctionEntities())
            {
                try
                {
                    context.SanPhams.Add(sanPham);
                    context.SaveChanges();
                    return true;
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                    return false;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public static bool Update(SanPham sanPham)
        {
            using (var context = new EnglishAuctionEntities())
            {
                try
                {
                    var existing = context.SanPhams.Find(sanPham.Id);
                    existing.GiaSan = sanPham.GiaSan;
                    existing.HinhAnh = sanPham.HinhAnh;
                    existing.isDelete = sanPham.isDelete;
                    existing.LoaiSanPhamId = sanPham.LoaiSanPhamId;
                    existing.MaSanPham = sanPham.MaSanPham;
                    existing.MoTa = sanPham.MoTa;
                    existing.NgayCapNhat = sanPham.NgayCapNhat;
                    existing.NgayLenSan = sanPham.NgayLenSan;
                    existing.NgayTao = sanPham.NgayTao;
                    existing.NguoiDungId = sanPham.NguoiDungId;
                    existing.TenSanPham = sanPham.TenSanPham;
                    existing.GiaThiTruong = sanPham.GiaThiTruong;
                    existing.ThoiGianDauGia = sanPham.ThoiGianDauGia;
                    existing.TinhTrang = sanPham.TinhTrang;
                    existing.NgayCapNhat = DateTime.Now;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public static bool Delete(int id)
        {
            using (var context = new EnglishAuctionEntities())
            {
                try
                {
                    SanPham sanPhamXoa = context.SanPhams.Find(id);
                    context.SanPhams.Remove(sanPhamXoa);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }
    }
}